const catchUtil = {
	// 设置/添加 cookie
	setCookie: (name, value, {
		maxAge,
		domain,
		path,
		secure
	} = {}) => {
		let cookieText = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
		if (typeof maxAge === 'number') {
			cookieText += `; max-age=${maxAge}`;
		}
		if (domain) {
			cookieText += `; domain=${domain}`;
		}
		if (path) {
			cookieText += `; path=${path}`;
		}
		if (secure) {
			cookieText += `; secure=${secure}`;
		}
		uni.setStorageSync('cookie_' + name, value);
	},

	// 获取 cookie
	getCookie: (name) => {
		if (name) {
			name = decodeURIComponent(name);
			const cookieList = uni.getStorageSync('allCookies') || [];
			for (let val of cookieList) {
				if (name === decodeURIComponent(val.split('=')[0])) {
					return decodeURIComponent(val.split('=')[1]);
				}
			}
		}
		return '';
	},

	// 删除 cookie

	setLocalStorage: (key, val) => {
		if (key && val) {
			uni.setStorageSync(key, JSON.stringify(val));
		}
	},

	getLocalStorage: (key) => {
		const val = uni.getStorageSync(key);
		if (val) {
			return JSON.parse(val);
		}
	},

	setSessionStorage: (key, val) => {
		if (key && val) {
			uni.setStorageSync(key, JSON.stringify(val));
		}
	},

	getSessionStorage: (key) => {
		const val = uni.getStorageSync(key);
		if (val) {
			return JSON.parse(val);
		} else {
			return {};
		}
	}
};

export default catchUtil;